; (function($) {
    var $container;
    var nextID, slides;
    var attachEvents = function() {
        $container.bind("updateSize orientationchange",
        function(event) {
            $container.height($(window).height());
            updateSlideSize();
        });
        var updateSlideSize = function(slide) {
            if (slide === undefined) {
                var slide = $container.data("currentSlide");
            }
            if (slide && slide.$img) {
                var wh = $(window).height();
                var ww = $(window).width();
                if ((ww / wh) > (slide.$img.width() / slide.$img.height())) {
                    slide.$img.css({
                        "height": "auto",
                        "width": "auto"
                    });
                } else {
                    slide.$img.css({
                        "height": "auto",
                        "width": "auto"
                    });
                }
                slide.$img.css({
                    "margin-left": "-" + (0.5 * slide.$img.width()) + "px",
                    "margin-top": "-" + (0.5 * slide.$img.height()) + "px"
                });
            }
        }
        $(window).bind("resize",
        function() {
            $container.trigger("updateSize");
        });
        var isLoading = false;
        $container.bind("showSlide",
        function(event, newSlide) {
            if (!isLoading) {
                var oldSlide = $container.data("currentSlide");
                if (! ("$img" in newSlide)) {
                    isLoading = true;
                    $container.trigger("startLoading");
                    newSlide.$img = $('<img class="slide">').css({
                        "position": "absolute",
                        "left": "50%",
                        "top": "50%"
                    }).hide().load(function() {
                        isLoading = false;
                        $container.trigger("stopLoading");
                        updateSlideSize(newSlide);
                        changeSlide(oldSlide, newSlide);
                    }).error(function() {
                        isLoading = false;
                        newSlide.error = true;
                        $container.trigger("stopLoading").trigger("error", newSlide);
                    }).attr("src", newSlide.image);
                    $container.append(newSlide.$img);
                } else {
                    changeSlide(oldSlide, newSlide);
                }
            }
        });
        $container.bind("prevSlide nextSlide",
        function(event) {

    
            nextID, slides = $container.data("slides"),
            currentSlide = $container.data("currentSlide"),
            currentID = currentSlide && currentSlide.id || 0;
            
            if (event.type == "nextSlide") {
                nextID = (currentID + 1) % slides.length;
            } else {
                nextID = (currentID - 1 + slides.length) % slides.length;
            }
            $container.trigger("showSlide", slides[nextID]);
            $container.attr({'data-i':nextID,'data-j':slides.length});

            $('.pagination i').html(nextID+1);
            $('.pagination span').html(slides.length);


        });
        

        var changeSlide = function(oldSlide, newSlide) {
            if (oldSlide !== undefined) {
                $container.trigger("endOfSlide", oldSlide);
                oldSlide.$img.fadeOut();
            }
            if (newSlide.$img && !newSlide.error) {
                newSlide.$img.fadeIn(function() {
                    $container.trigger("startOfSlide", newSlide);
                });
            } else {
                $container.trigger("startOfSlide", newSlide);
            }
            $container.data("currentSlide", newSlide);
        }
        var keyFunc = function(event) {
            if (event.keyCode == 27) {
                $container.trigger("close");
            }
            if (event.keyCode == 37) {
                $container.trigger("prevSlide");
            }
            if (event.keyCode == 39) {
                $container.trigger("nextSlide");
            }
        }
        $container.bind("close",
        function() {
            var options = $container.data("options");
            var oldSlide = $container.data("currentSlide");
            oldSlide && oldSlide.$img && oldSlide.$img.hide();
            $container.trigger("endOfSlide", oldSlide);
            $(document).unbind("keydown", keyFunc);
            if (options.useFullScreen) {
                if (document.cancelFullScreen) {
                    document.cancelFullScreen();
                }
                if (document.mozCancelFullScreen) {
                    $("html").css("overflow", "auto");
                    $(document).scrollTop($container.data("mozScrollTop"));
                    document.mozCancelFullScreen();
                }
                if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                }
                document.removeEventListener('fullscreenchange', changeFullScreenHandler);
                document.removeEventListener('mozfullscreenchange', changeFullScreenHandler);
                document.removeEventListener('webkitfullscreenchange', changeFullScreenHandler);
            } else {
                $container.data("hiddenElements").show();
                $(window).scrollTop($container.data("originalScrollTop"));
            }
            $container.removeData("currentSlide slides width height originalScrollTop hiddenElements").hide();
        });
        function changeFullScreenHandler(event) {
            if ($container.data("isFullScreen")) {
                $container.trigger("close");
            }
            $container.data("isFullScreen", true);
        }
        var firstrun = true;
        $container.bind("show",
        function(event, rel, slide) {
            var options = $container.data("options");
            var slideshows = $container.data("slideshows");
            var slides = slideshows[rel];
            $container.data("slides", slides);
            $container.trigger("updateSize");
            $(document).bind("keydown", keyFunc);
            if (options.useFullScreen) {
                con = $container[0];
                if (con.requestFullScreen) {
                    con.requestFullScreen();
                    document.addEventListener('fullscreenchange', changeFullScreenHandler);
                }
                if (con.mozRequestFullScreen) {
                    con.mozRequestFullScreen();
                    document.addEventListener('mozfullscreenchange', changeFullScreenHandler);
                    $container.data("mozScrollTop", $(document).scrollTop());
                    $("html").css("overflow", "hidden");
                }
                if (con.webkitRequestFullScreen) {
                    con.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
                    document.addEventListener('webkitfullscreenchange', changeFullScreenHandler);
                }
                $container.data("isFullScreen", false);
            }
            if (firstrun) {
                $container.trigger("init");
                firstrun = false;
            }
            if (!options.useFullScreen) {
                $container.data("hiddenElements", $('body > #fullPicBox').filter(function() {
                    return $(this).css("display") != "none";                       //$('body > *') 所有DIV都加了display:none;
                }).hide());
            }
            if (!$container.data("originalScrollTop")) {
                $container.data("originalScrollTop", $(window).scrollTop());
            }
            $container.show();
            $container.trigger("showSlide", slide);
        });
    }
    $.fn.fullscreenslides = function(options) {
        $container = $('#fullPicBox');
        if ($container.length == 0) {
            $container = $('<div id="fullPicBox">').hide();
            $("body").append($container);
            attachEvents();
        }
        var options = $.extend({
            "bgColor": "#000",
            "useFullScreen": false,
            "startSlide": 0
        },
        options || {});
        options.useFullScreen = options.useFullScreen && !!($container[0].requestFullScreen || $container[0].mozRequestFullScreen || $container[0].webkitRequestFullScreen);
        $container.data("options", options);
        $container.css({
            "position": "fixed",                //屏幕定位
            "top": "0px",
            "left": "0px",
            "width": "100%",
            "z-index": "1000",
            "text-align": "center",
            "background-color": options.bgColor
        });
        var slideshows = {};
        this.each(function() {
            var link = $(this).parents("a")[0];
            if (!link.rel) link.setAttribute("rel", "__all__");
            var slide = {
                image: link.href,
                title: link.title,
                rel: link.rel
            };
            slide.data = $.extend({},
            $(this).data(), $(link).data());
            slideshows[slide.rel] = slideshows[slide.rel] || [];
            slideshows[slide.rel].push(slide);
            slide.id = slideshows[slide.rel].length - 1;
            $(link).data("slide", slide);
            $(link).click(function(event) {
                $container.trigger("show", [this.rel, $(this).data("slide")]);
                event.preventDefault();
            });
        });
        $container.data("slideshows", slideshows);
    }
})(jQuery);